package com.zx.tv.ui;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.widget.Toast;
import android.widget.ZoomControls;

import com.zx.base.ctrl.Constant;
import com.zx.base.ctrl.KeyTable;
import com.zx.base.http.URLHelper;
import com.zx.base.lazylist.ImageZoomView;
import com.zx.base.lazylist.SimpleZoomListener;
import com.zx.base.lazylist.ZoomState;
import com.zx.tv.R;

public class Detail extends Activity {
	/** Called when the activity is first created. */
	ImageZoomView mZoomView;
	Bitmap image;
	ZoomState mZoomState;
	SimpleZoomListener mZoomListener;
	private int zoom=0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.imagedetail);

		String imgname = getIntent().getStringExtra("imgId");
		String url = URLHelper.mPrefixImg + imgname;
		new InfoTask().execute(url);
		mZoomView = (ImageZoomView) findViewById(R.id.pic);
		
//		zoomCtrl.setOnZoomInClickListener(new OnClickListener() {
//
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				float z = mZoomState.getZoom() + 0.25f;
//				mZoomState.setZoom(z);
//				mZoomState.notifyObservers();
//			}
//
//		});
//		zoomCtrl.setOnZoomOutClickListener(new OnClickListener() {
//
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				float z = mZoomState.getZoom() - 0.25f;
//				mZoomState.setZoom(z);
//				mZoomState.notifyObservers();
//			}
//
//		});
	}

	private void resetZoomState() {
		mZoomState.setPanX(0.5f);
		mZoomState.setPanY(0.5f);

		final int mWidth = image.getWidth();
		final int vWidth = mZoomView.getWidth();
		mZoomState.setZoom(1f);
		mZoomState.notifyObservers();

	}

	public Bitmap drawableToBitmap(Drawable drawable) {
		Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
		return bitmap;
	}

	class InfoTask extends AsyncTask<String, Void, Drawable> {

		@Override
		protected Drawable doInBackground(String... params) {

			return loadImageFromUrl(params[0]);// 网络操作，向服务器发送登录请求，获取返回信息

		}

		@Override
		protected void onPostExecute(Drawable result) {

			if (Constant.debug)
				Log.d("TAG", "login result = " + result);
			if (result == null || result.equals("timeout")) {
				Toast toast = Toast.makeText(Detail.this, "网络超时。",
						Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();

			} else {
				// 发送解析数据 消息
				image = drawableToBitmap(result);
				
				mZoomView.setImage(image);
				mZoomState = new ZoomState();
				mZoomView.setZoomState(mZoomState);
				mZoomListener = new SimpleZoomListener();
				mZoomListener.setZoomState(mZoomState);
				mZoomView.setOnTouchListener(mZoomListener);
				resetZoomState();
			}

		}
		
		public Drawable loadImageFromUrl(String url) {
			URL m;
			InputStream i = null;
			try {
				m = new URL(url);
				i = (InputStream) m.getContent();
			} catch (MalformedURLException e1) {
				e1.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			Drawable d = Drawable.createFromStream(i, "src");
			return d;
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		 if (keyCode == KeyTable.KEY_DOWN && zoom>-3) {
			 
				float z = mZoomState.getZoom() - 0.25f;
				mZoomState.setZoom(z);
				mZoomState.notifyObservers();
				zoom--;
		 }else if(keyCode == KeyTable.KEY_UP  && zoom<3 ){
				float z = mZoomState.getZoom() + 0.25f;
				mZoomState.setZoom(z);
				mZoomState.notifyObservers();
				zoom++;
		 }else if(keyCode == KeyTable.KEY_ENTER){
			 resetZoomState();
		 }
		return super.onKeyDown(keyCode, event);
	}

}
